# -*- coding: utf-8 -*- 

import logging
import datetime
from google.appengine.ext import db

def _today():
    now = _now()
    return datetime.date(now.year, now.month, now.day)
def _now():
    return datetime.datetime.utcnow() + datetime.timedelta(hours=+8)

class GroupSite(db.Model):
    code = db.StringProperty()
    name = db.StringProperty()
    link = db.LinkProperty()
    pop = db.IntegerProperty(default=0)
    
    @property
    def key_value(self):
        return self.key().name()
    
    @property
    def today_infos(self):
        # get date
        today = _today()
        yesterday = today + datetime.timedelta(days=-1)

        # get today's group
        return self.groups.filter('date >', yesterday)

class GroupInfo(db.Model):
    site = db.ReferenceProperty(GroupSite, collection_name='groups')
    title = db.StringProperty(multiline=True)
    date = db.DateProperty(auto_now_add=True)
    current_price = db.FloatProperty(default=0.0)
    original_price = db.FloatProperty(default=0.0)
    discount = db.FloatProperty(default=0.0)
    picture = db.LinkProperty()
    link = db.LinkProperty()
    sold_total = db.IntegerProperty(default=0)

    def create(self):
        self.date = _today()
        self.put()

    @property
    def title_short(self):
        return "%s..."%self.title[:30]

    @property
    def date_rfc(self):
        return self.date.strftime("%a, %d %b %Y %H:%M:%S +0000")

